package net.hnjdzy.newsclient;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.AsyncTask;
import android.os.Bundle;

import com.viewpagerindicator.TabPageIndicator;


import net.hnjdzy.newsclient.entity.Topic;
import net.hnjdzy.newsclient.manager.TopicManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    List<NewsListFragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //测试TopicList
        List<Topic> topicList = new ArrayList<>();
        Topic topic = new Topic();
        topic.setTid(1);
        topic.setTname("国内");
        topicList.add(topic);

        topic = new Topic();
        topic.setTid(2);
        topic.setTname("国际");
        topicList.add(topic);

        topic = new Topic();
        topic.setTid(3);
        topic.setTname("体育");
        topicList.add(topic);

        initViewPager(topicList);
        TopicTask topicTask =new TopicTask();
        topicTask.execute();
    }
    class TopicTask extends AsyncTask<Void,Void,List<Topic>>{
        @Override
        protected void onPostExecute(List<Topic> topics) {
            initViewPager(topics);
        }

        @Override
        protected List<Topic> doInBackground(Void... voids) {
            TopicManager topicManager =new TopicManager();
            return  topicManager.getTopicList();
        }
    }

    //初始化ViewPager
    private void initViewPager(List<Topic> topics) {
        ViewPager viewPager = findViewById(R.id.pager);

        NewsTopicAdapter adapter = new NewsTopicAdapter(getSupportFragmentManager(),topics);
        viewPager.setAdapter(adapter);

        TabPageIndicator indicator = findViewById(R.id.indicator);
        indicator.setViewPager(viewPager);
        indicator.notifyDataSetChanged();
    }

    //适配器类
    class NewsTopicAdapter extends FragmentPagerAdapter {
        List<Topic> topicList;

        public NewsTopicAdapter(FragmentManager fm, List<Topic> topics) {
            super(fm);

            fragments.clear();  //清除原有的数据
            topicList = topics;
            for(int i=0;i<topicList.size();i++){
                fragments.add(new NewsListFragment(topicList.get(i).getTid())); //传入新闻标题的TID
            }
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return topicList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {   // 页标题
            return topicList.get(position).getTname();
        }
    }

}
